/*
 Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
 Original sources are available at www.latestbit.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 StdSymmetricEncrypter.hpp. Created on: 01.09.2009 16:42:30
 */

#ifndef STDSYMMETRICENCRYPTER_HPP_
#define STDSYMMETRICENCRYPTER_HPP_

#include "smartobjects/SmartObjectsDefs.hpp"
#include "ISymmetricDataEncrypter.hpp"
#include <openssl/rand.h>
#include <openssl/aes.h>
#include <openssl/evp.h>

namespace smartobjects {

class StdSymmetricEncrypter : public ISymmetricDataEncrypter {
public:
	SO_DLLPRFX StdSymmetricEncrypter();
	SO_DLLPRFX virtual ~StdSymmetricEncrypter();

	SO_DLLPRFX virtual void initCipher( SecretKey* poKey );
	SO_DLLPRFX virtual SecretKey* generateSecret();

	SO_DLLPRFX virtual SecretKey* decodeSecretKey(ByteBuffer* poBuffer);
	SO_DLLPRFX virtual void encodeSecretKey( SecretKey* poKey, ByteBuffer* poBuffer );

	SO_DLLPRFX virtual void encrypt(ByteBuffer* poInputData, ByteBuffer* poOutput);
	SO_DLLPRFX virtual void decrypt(ByteBuffer* poEncryptedData, ByteBuffer* poOutput);
	SO_DLLPRFX virtual const std::string& getAlgorithm() const;

protected:
	SO_DLLPRFX static const std::string ALGORITHM_NAME;
	SO_DLLPRFX static const unsigned long KEY_LENGTH;

	EVP_CIPHER_CTX m_oEncrypterCtx;
	EVP_CIPHER_CTX m_oDecrypterCtx;

	boost::recursive_mutex m_mxEncrypter;
	boost::recursive_mutex m_mxDecrypter;

	SecretKey* m_poKey;
};

}

#endif /* STDSYMMETRICENCRYPTER_HPP_ */
